﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading;

namespace SvD_Templates
{
    public partial class netTools : Form
    {
        protected override bool ProcessDialogKey(Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Escape:
                    this.Dispose();
                    break;

                case Keys.F1:
                    cajaIP.ResetText();
                    cajaHost.ResetText();
                    cajaResultados.ResetText();
                    break;
            }

            return base.ProcessDialogKey(keyData);
        }

        public netTools()
        {
            InitializeComponent();
        }

        private void pingIP_Click(object sender, EventArgs e)
        {
            Ping ping = new Ping();

            try
            {
                int timeout = 5000;
                byte[] buffer = Encoding.ASCII.GetBytes("ooooooooooooooooooooooo");
                PingOptions options = new PingOptions(64, true);

                AutoResetEvent waiter = new AutoResetEvent(false);
                ping.PingCompleted += new PingCompletedEventHandler(ping_PingCompleted);
                ping.SendAsync(cajaIP.Text, timeout, buffer, options, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void ping_PingCompleted(object sender, PingCompletedEventArgs e)
        {
            try
            {
                string output = "** Ping Results **";
                output += "\r\nIP: " + cajaIP.Text;
                output += "\r\nHostname: " + getHost(cajaIP.Text);
                output += "\r\nStatus : " + e.Reply.Status.ToString();
                output += "\r\nAddress : " + e.Reply.Address.ToString();
                output += "\r\nRound Trip Time : " + e.Reply.RoundtripTime.ToString() + "ms";
                cajaResultados.Text = output;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cajaIP_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
                cajaHost.Text = getHost(cajaIP.Text); 
        }

        private void cajaHost_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
                cajaIP.Text = getIP(cajaHost.Text);
        }

        public string getHost(string ip)
        {
            try
            {
                IPHostEntry IpEntry = Dns.GetHostByAddress(ip);
                return IpEntry.HostName.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }

        public string getIP(string hostname)
        {
            try
            {
                IPAddress[] addresslist = Dns.GetHostAddresses(hostname);

                foreach (IPAddress theaddress in addresslist)
                {
                    return theaddress.ToString();
                }

                return "n/a";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return "n/a";
            }
        }

        private void netTools_Load(object sender, EventArgs e)
        {

        }

        private void sendResults_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(cajaResultados.Text);
        }
    }
}
